<?php
/**
 * UrlRoute.php
 * 
 * @category   Naneau
 * @package    Naneau_Controller_Helper
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** Zend_Controller_Action_Helper_Abstract */
require_once 'Zend/Controller/Action/Helper/Abstract.php';


/**
 * Naneau_Controller_Action_Helper_UrlRoute
 * 
 * build urls according to routes - like the redirector view helper but without the redirect
 *
 * @category   Naneau
 * @package    Naneau_Controller_Helper
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Action_Helper_UrlRoute extends Zend_Controller_Action_Helper_Abstract {

    /**
     * convenience method, for direct access
     */
    public function direct() {
        $args = func_get_args();
        return call_user_func_array(array($this, 'get'), $args);
    }

    /**
     * get a url based on a route
     *
     * @param array $options
     * @param string $routeName
     * @param bool $reset
     * @return string
     */
    public function get($options = null, $routeName = 'default', $reset = false) {
        $options = (array) $options;
        //allow for emtpy options
        
        $frontController = Zend_Controller_Front::getInstance();
        //front controller
        $router = $frontController->getRouter();
        //router

        if (!$router->hasRoute($routeName)) {
            throw new Exception('route not found: ' . $routeName);
        }
        //check if route exists

        $route = $router->getRoute($routeName);
        //the actual route

        $url = rtrim($frontController->getBaseUrl(), '/') . '/';
        $url .= $route->assemble($options, $reset);
        //the actual url

        return $url;
    }
}